/*!
 * 本地存储
 */
export default {
    /**
     * 写入Storage
     * @param {string} key 值名称
     * @param {string} value 值
     * @param {string} exp 过期时间(12小时)
     * @return {void}
     */
    set(key, value, exp = 60 * 60 * 12) {
        let timestamp = Date.now();
        let data = JSON.stringify({ value, exp, timestamp });
        localStorage.setItem(key, data);
    },
    /**
     * 获取Storage
     * @param {string} key 值名称
     * @return {string}
     */
    get(key) {
        let data = localStorage.getItem(key);
        if (!data) {
            return false;
        }

        data = JSON.parse(data);

        let exp = data.exp * 1000;
        let { timestamp } = data;
        let { value } = data;
        let dataNow = Date.now();
        if (dataNow > timestamp + exp) {
            this.remove(key);
            return false;
        }
        return value;
    },
    /**
     * 删除Storage
     * @param {string} key 值名称
     * @return {void}
     */
    remove(key) {
        localStorage.removeItem(key);
    }
};